package com.JingYan.util;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Component;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * 邮件发送工具类
 *
 */

@Slf4j
@Component
public class SendEmailUtils {

    final
    JavaMailSender javaMailSender;
    @Autowired
    private Environment env;

    public SendEmailUtils(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }


    /**
     * 发送文本消息
     * @param subject 邮件主题
     * @param mailFrom 发件人
     * @param content 邮件内容
     * @param toList 收件人
     * @param ccList 抄送人
     */
    public void sendEmail(String subject, String mailFrom, String content, List<String> toList, List<String> ccList) {
        //收件人地址
        InternetAddress[] addressesTo = new InternetAddress[toList.size()];
        if(toList != null && toList.size() > 0){
            for(int i=0;i<toList.size();i++){
                String toAdd = toList.get(i);
                InternetAddress addressTo = null;
                try {
                    addressTo = new InternetAddress(toAdd);
                    addressesTo[i] = addressTo;
                } catch (AddressException e) {
                    e.printStackTrace();
                }
            }
        }
        //抄送人地址
        InternetAddress[] addressesCc = new InternetAddress[ccList.size()];
        if(ccList != null && ccList.size() > 0){
            for(int i=0;i<ccList.size();i++){
                String ccAdd = ccList.get(i);
                InternetAddress address = null;
                try {
                    address = new InternetAddress(ccAdd);
                    addressesCc[i] = address;
                } catch (AddressException e) {
                    e.printStackTrace();
                }
            }
        }

        MimeMessagePreparator preparator = mimeMessage -> {
            mimeMessage.setFrom(mailFrom);
            mimeMessage.setSubject(subject);
            mimeMessage.setText(content);
            mimeMessage.setRecipients(Message.RecipientType.TO, addressesTo);
            mimeMessage.setRecipients(Message.RecipientType.CC, addressesCc);
        };
        /*MimeMessagePreparator preparator = new MimeMessagePreparator() {
            @Override
            public void prepare(MimeMessage mimeMessage) throws Exception {
                mimeMessage.setFrom(from);
                mimeMessage.setSubject(subject);
                mimeMessage.setText(content);
                mimeMessage.setRecipients(Message.RecipientType.TO, addressesTo);
                mimeMessage.setRecipients(Message.RecipientType.CC, addressesCc);
            }
        };*/
        javaMailSender.send(preparator);

    }

    /**
     *  发送简单邮件
     */
    public void sendSimpleMail(String mailSubject,String mailFrom,String mailTo,String mailText) {
        try{
            SimpleMailMessage msg = new SimpleMailMessage();    //构建一个邮件对象
            msg.setSubject(mailSubject); // 设置邮件主题
            msg.setFrom(mailFrom); // 设置邮箱发送者
            msg.setTo(mailTo); // 设置邮件接收者，可以有多个接收者
            msg.setSentDate(new Date(System.currentTimeMillis()));    // 设置邮件发送日期
            msg.setText(mailText);   // 设置邮件的正文
            javaMailSender.send(msg);
        } catch (Exception e){
            // e.printStackTrace();
            log.error("邮件发送失败!",e);
        }
    }

    /**
     * 发送带附件的邮件
     * @param mailSubject 邮件主题
     * @param mailFrom      发件人
     * @param mailTo        收件人
     * @param mailText      邮件内容
     * @param attachmentFilename 附件名
     * @param file           附件
     * @throws MessagingException
     */
    public void sendAttachFileMail(String mailSubject,String mailFrom,String mailTo,String mailText,String attachmentFilename,File file) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
        helper.setSubject(mailSubject);
        helper.setFrom(mailFrom);
        helper.setTo(mailTo);
        helper.setSentDate(new Date(System.currentTimeMillis()));
        helper.setText(mailText);
        helper.addAttachment(attachmentFilename,file);
        javaMailSender.send(mimeMessage);
    }

}
